package com.tianhao.luo.factory;

import com.tianhao.luo.constant.PlantRefence;
import com.tianhao.luo.plant.Plant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Date 2019/12/9
 * @Author tianhao.luo
 * @Description
 */
public class SimpleFactory {
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleFactory.class);

    public static Plant createPlant(PlantRefence plantRefence) {
        Plant plant = null;
        try {
            Class<?> aClass = Class.forName(plantRefence.getPlantRefPath());
            Constructor<?> constructor = aClass.getConstructor();
            plant = (Plant) constructor.newInstance();
            Method setName = aClass.getDeclaredMethod("setName",String.class);
            setName.invoke(plant,plantRefence.getPlantName());
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
            LOGGER.error("工程初始化失败{}",e);
        }
        return plant;
    }
}
